package com.ruoyi.report_ljlm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_ljlm.domain.HdbgBsDetail;
import com.ruoyi.report_ljlm.domain.vo.HdbgBsDetailVo;
import com.ruoyi.report_ljlm.mapper.HdbgBsDetailMapper;
import com.ruoyi.syrw.annotation.ComputeReportResult;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_ljlm.domain.bo.HdbgBsInfoBo;
import com.ruoyi.report_ljlm.domain.vo.HdbgBsInfoVo;
import com.ruoyi.report_ljlm.domain.HdbgBsInfo;
import com.ruoyi.report_ljlm.mapper.HdbgBsInfoMapper;
import com.ruoyi.report_ljlm.service.IHdbgBsInfoService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 路基路面厚度测试试验检测报告Service业务层处理
 *
 * @author llb
 * @date 2024-07-10
 */
@RequiredArgsConstructor
@Service
public class HdbgBsInfoServiceImpl implements IHdbgBsInfoService {

    private final HdbgBsInfoMapper baseMapper;
    private final HdbgBsDetailMapper detailMapper;

    /**
     * 查询路基路面厚度测试试验检测报告
     */
    @Override
    @FIllBasicInfo
    public HdbgBsInfoVo queryById(Long excelId) {
        HdbgBsInfoVo hdbgBsInfoVo = baseMapper.selectVoById(excelId);
        List<HdbgBsDetailVo> detailList = detailMapper.selectVoList(
            new LambdaQueryWrapper<HdbgBsDetail>().eq(HdbgBsDetail::getExcelId, excelId));
        if (CollUtil.isNotEmpty(detailList)) {
            hdbgBsInfoVo.setDetailList(detailList);
        }
        return hdbgBsInfoVo;
    }

    /**
     * 查询路基路面厚度测试试验检测报告列表
     */
    @Override
    public TableDataInfo<HdbgBsInfoVo> queryPageList(HdbgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HdbgBsInfo> lqw = buildQueryWrapper(bo);
        Page<HdbgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询路基路面厚度测试试验检测报告列表
     */
    @Override
    public List<HdbgBsInfoVo> queryList(HdbgBsInfoBo bo) {
        LambdaQueryWrapper<HdbgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HdbgBsInfo> buildQueryWrapper(HdbgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HdbgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, HdbgBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, HdbgBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), HdbgBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getBaogbh()), HdbgBsInfo::getBaogbh, bo.getBaogbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShigwtdw()), HdbgBsInfo::getShigwtdw, bo.getShigwtdw());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), HdbgBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), HdbgBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), HdbgBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), HdbgBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), HdbgBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), HdbgBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), HdbgBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), HdbgBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), HdbgBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), HdbgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(bo.getHoudsjz() != null, HdbgBsInfo::getHoudsjz, bo.getHoudsjz());
        lqw.eq(bo.getYunxpc() != null, HdbgBsInfo::getYunxpc, bo.getYunxpc());
        lqw.eq(bo.getPingjz() != null, HdbgBsInfo::getPingjz, bo.getPingjz());
        lqw.eq(bo.getBzc() != null, HdbgBsInfo::getBzc, bo.getBzc());
        lqw.eq(bo.getHouddbz() != null, HdbgBsInfo::getHouddbz, bo.getHouddbz());
        lqw.eq(bo.getJiancds() != null, HdbgBsInfo::getJiancds, bo.getJiancds());
        lqw.eq(bo.getHegds() != null, HdbgBsInfo::getHegds, bo.getHegds());
        lqw.eq(bo.getHegl() != null, HdbgBsInfo::getHegl, bo.getHegl());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), HdbgBsInfo::getJiancjl, bo.getJiancjl());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), HdbgBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getBgFlag()), HdbgBsInfo::getBgFlag, bo.getBgFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), HdbgBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), HdbgBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), HdbgBsInfo::getFuh, bo.getFuh());
        lqw.eq(StringUtils.isNotBlank(bo.getRiq()), HdbgBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), HdbgBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), HdbgBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), HdbgBsInfo::getJiancyjIds, bo.getJiancyjIds());
        return lqw;
    }

    /**
     * 新增路基路面厚度测试试验检测报告
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(HdbgBsInfoBo bo) {
        HdbgBsInfo add = BeanUtil.toBean(bo, HdbgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            ArrayList<HdbgBsDetail> details = new ArrayList<>(36);
            for (int i = 1; i <= 36; i++) {
                details.add(new HdbgBsDetail(bo.getExcelId()));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改路基路面厚度测试试验检测报告
     */
    @Override
    @DSTransactional
    @ComputeReportResult
    public Boolean updateByBo(HdbgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        HdbgBsInfo update = BeanUtil.toBean(bo, HdbgBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getDetailList(), HdbgBsDetail.class));
        }
        return flag;
    }

    private void fillExcelResult(HdbgBsInfoBo bo) {
        if (!BigDecimalUtil.isAnyOneEmpty(bo.getYunxpc(), bo.getHoudsjz(), bo.getHouddbz())) {
            BigDecimal abs = bo.getYunxpc().abs();
            boolean greaterOrEqual = BigDecimalUtil.isGreaterOrEqual(bo.getHouddbz(), bo.getHoudsjz().subtract(abs));
            if (!greaterOrEqual) {
                bo.setExcelResult(2);
                return;
            }
        }
        bo.setExcelResult(1);
    }

    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HdbgBsInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除路基路面厚度测试试验检测报告
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public HdbgBsInfoVo jcjl(HdbgBsInfoVo vo) {
        StringBuilder result = new StringBuilder("检测结论：经检测，所检指标均符合JTG F40-2004《公路沥青路面施工技术规范》，JTG F80/1-2017《公路工程质量检验评定标准》及设计文件要求。");
        vo.setJiancjl(result.toString());
        return vo;
    }
}
